<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <el-input v-model="listQuery.param.modelerKey" placeholder="模型KEY" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilterQuery" />
      <el-input v-model="listQuery.param.modelerName" placeholder="模型名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilterQuery" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
        重置
      </el-button>
    </div>
    <div class="filter-container">
      <div style="float: left;">
        <el-button class="filter-item" type="primary" plain icon="el-icon-plus" @click="handleCreate">
          新增
        </el-button>
      </div>
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />
      <el-table-column label="模型ID" min-width="100px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="模型KEY" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerKey }}</span>
        </template>
      </el-table-column>
      <el-table-column label="模型名称" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类别" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerCategory }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版本号" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerVersion }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.modelerDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="110px" align="center">
        <template v-slot="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" min-width="90px" align="center">
        <template v-slot="{row}">
          <span>{{ row.createdBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160px" align="center">
        <template v-slot="{row}">
          <span>{{ row.createdTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="100px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <!--          <el-tooltip content="详情" placement="top">-->
          <!--            <el-button type="text" icon="el-icon-document" @click="handleDetail(row, $index)" />-->
          <!--          </el-tooltip>-->
          <el-tooltip content="更多" placement="top">
            <el-dropdown style="margin-left: 10px;" @command="handleClick($event, row, $index)">
              <el-button type="text">
                <i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu>
                <el-dropdown-item command="edit"><i class="el-icon-edit"> 修改</i></el-dropdown-item>
                <el-dropdown-item command="del"><i class="el-icon-delete"> 删除</i></el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />

    <!--画图-->
    <modeler-design ref="modelerDesign" :xml="modelerData.xml" :show="modelerData.show" />
  </div>
</template>

<script>
import { getPageList } from '@/views/flow/api/modeler'
import permission from '@/directive/permission/index' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import ModelerDesign from './modeler.vue'

export default {
  // 当前页面名称（组件）
  name: 'FlowModeler',
  // 引用组件
  components: { ModelerDesign, Pagination },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          modelerKey: undefined,
          modelerName: undefined
        }
      },
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {
        update: '修改',
        create: '新增',
        detail: '详情'
      },
      // 权限标识
      permission: {
        cancel: ['flow:inst:cancel'],
        del: ['flow:inst:del']
      },
      // 表单校验规则
      rules: {},
      modelerData: {
        show: false,
        xml: ''
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.handleInit()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.loading = true
      getPageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.listQuery.param.modelerKey = undefined
      this.listQuery.param.modelerName = undefined
    },
    // 更多点击操作项
    handleClick(item, row, index) {
      // 编辑
      if (item === 'edit') {
        this.handleUpdate(row, index)
      }
      // 删除
      if (item === 'del') {
        this.handleDelete(row, index)
      }
    },
    // 重置表单
    handleRestFormData() {
      this.formData = {
        id: undefined,
        postId: undefined,
        postName: undefined,
        status: 'Y',
        remark: ''
      }
    },
    // 新增
    handleCreate() {
      this.modelerData.xml = ''
      this.modelerData.show = true
      this.$nextTick(() => {
        this.$refs['modelerDesign'].init()
      })
      // this.state = 'create'
      // this.visible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    // 修改
    handleUpdate(row, index) {
      this.modelerData.xml = row.modelerXml
      this.modelerData.show = true
      this.$nextTick(() => {
        this.$refs['modelerDesign'].init()
      })

      // this.state = 'update'
      // this.visible = true
      //
      // // 创建一个新的对象赋值
      // this.formData = Object.assign({}, row)
      // // 清理掉多余的属性值，避免数据不对问题
      // delete this.formData.createdBy
      // delete this.formData.createdTime
      // delete this.formData.updatedBy
      // delete this.formData.updatedTime
      // delete this.formData.tenantId
      // delete this.formData.revision
      // delete this.formData.deleted
      //
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    // 删除
    handleDelete(row, index) {
      // this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   handleDelete({ postId: row.postId }).then((res) => {
      //     //  刷新table
      //     this.handleFilterQuery()
      //     this.$message({
      //       showClose: true,
      //       message: res.msg,
      //       type: 'success',
      //       duration: 4000
      //     })
      //   })
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除'
      //   })
      // })
    },
    // 表单提交
    handleSubmitFormData() {
      // this.$refs['dataForm'].validate((valid) => {
      //   if (valid) {
      //     handleSave(this.formData).then((res) => {
      //       this.handleRestFormData()
      //       //  刷新table
      //       this.handleFilterQuery()
      //       this.visible = false
      //       this.$message({
      //         showClose: true,
      //         message: res.msg,
      //         type: 'success',
      //         duration: 4000
      //       })
      //     })
      //   }
      // })
    },
    // 关闭弹出层
    handleClose() {
      this.handleRestFormData()
      this.visible = false
    }
  }
}
</script>
